﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace CRL
{
    public class ModelTracking
    {
        internal object origin;
        ParameCollection originValues;
        public ModelTracking(object obj)
        {
            obj.CheckNull("obj");
            origin = obj;
            originValues = new ParameCollection();
            var fields = TypeCache.GetTable(origin.GetType()).Fields;
            foreach (var f in fields)
            {
                var v = f.GetValue(origin);
                originValues[f.MemberName] = v;
            }
        }
        public ParameCollection GetDiff()
        {
            var newValues = new ParameCollection();
            var fields = TypeCache.GetTable(origin.GetType()).Fields;
            foreach (var f in fields)
            {
                var v = f.GetValue(origin);
                newValues[f.MemberName] = v;
            }
            foreach (var kv in new ParameCollection(newValues))
            {
                if (originValues[kv.Key]?.GetHashCode() == kv.Value?.GetHashCode())
                {
                    newValues.Remove(kv.Key);
                }
            }
            return newValues;
        }
    }
}
